package com.nodecollege.cloud.service.impl.deal;

import com.alibaba.fastjson.JSON;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.ChatRecord;
import com.nodecollege.cloud.common.model.vo.ChatRecordData;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.ChatService;
import com.nodecollege.cloud.service.RecordService;
import com.nodecollege.cloud.service.impl.ChatServiceAbstrat;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 获取历史消息
 *
 * @author LC
 * @date 2020/2/26 23:07
 */
@Slf4j
@Service
public class ChatGetRecordServiceImpl extends ChatServiceAbstrat implements ChatService {

    @Autowired
    private RecordService recordService;

    /**
     * 消息处理
     */
    @Override
    public void handler(String chatData, Channel channel) {
        log.info("ChatGetRecordServiceImpl handler");
        ChatRecordData recordData = JSON.parseObject(chatData, ChatRecordData.class);
        QueryVO<ChatRecord> queryVO = new QueryVO<>(new ChatRecord());
        queryVO.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        queryVO.setPageSize(recordData.getPageSize());
        queryVO.setPageNum(recordData.getPageNum());
        queryVO.getData().setReceiveUserId(recordData.getReceiveUserId());
        queryVO.getData().setSendTime(recordData.getSendTime());
        if (recordData.getGroupId() != null) {
            queryVO.getData().setGroupId(recordData.getGroupId());
        }else {
            queryVO.getData().setSendUserId(ThreadUserUtils.getLoginUser().getUserId());
        }
        NCResult<ChatRecord> record = recordService.getRecord(queryVO);
        // 给自己发送
        sendMsg(queryVO.getUserId(), record, 6);
    }
}
